package cn.itsource.org.controller;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.vali.ExcelUtils;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.dto.ShopDto;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /** 添加和修改 */
    @PutMapping
    private AjaxResult addAndUpdate(@RequestBody Shop shop){
        //如果没有id就是添加，有id就是修改
        if (shop.getId() == null){
            shopService.add(shop);
        }else {
            shopService.update(shop);
        }
        return new AjaxResult(true,"操作成功！");
    }

    /** 查询单个 */
    @GetMapping("/{id}")
    private Shop findOne(@PathVariable Long id){
        return shopService.findOne(id);
    }

    /** 查询所有 */
    @GetMapping
    private List<Shop> findAll(){
        return shopService.findAll();
    }

    /** 删除单个数据 */
    @DeleteMapping("/{id}")
    private AjaxResult delete(@PathVariable Long id){
        shopService.del(id);
        return new AjaxResult(true,"删除成功！");
    }

    /** 批量删除 */
    @PatchMapping
    private AjaxResult patchDel(@RequestBody Long[] ids){
        shopService.patchDel(ids);
        return new AjaxResult(true,"删除成功！");
    }

    /** 分页查询 */
    @PostMapping
    private PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    /** 店铺入驻 */
    @PostMapping("/settlement")
    private AjaxResult settlement(@RequestBody Shop shop){
        shopService.settlement(shop);
        return new AjaxResult();
    }

    /** 店铺人工审核 - 审核驳回 */
    @PostMapping("/audit/reject")
    private AjaxResult auditReject(@RequestBody ShopAuditLog log) throws MessagingException {
        shopService.auditReject(log);
        return new AjaxResult();
    }

    /** 店铺人工审核 - 审核通过 */
    @PostMapping("/audit/pass")
    private AjaxResult auditPass(@RequestBody ShopAuditLog log) throws MessagingException {
        shopService.auditPass(log);
        return new AjaxResult();
    }

    /** 店铺人工审核 - 激活邮件 */
    @GetMapping("/active/{id}")
    private String active(@PathVariable("id") Long id){
        try {
            shopService.active(id);
            return "激活成功！";
        } catch (BusinessException e) {
            e.printStackTrace();
            return "激活失败！";
        }
    }

    @GetMapping("/export")
    private void export(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        //调用工具类导出excel
        //(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response)
        //导出的数据      标题            sheetName页名称    导出的类型          文件名                响应对象
        //文件名如果不写后缀默认是xls，可以写上xlsx就是指定的xlsx类型
        ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        //             文件对象             忽略标题行数        护理表头行数             生成的对象类型
        List<Shop> list = ExcelUtils.importExcel(file,0,1,Shop.class);
        System.out.println("导入数据一共【"+list.size()+"】行");
        list.forEach(System.out::println);
        //保存到数据库中[提示：动态sql 批量插入]
    }

    /**
     * 后台首页柱状图
     * @return
     */
    @GetMapping("/echarts")
    private List<ShopDto> echarts(){
        return shopService.echarts();
    }

}
